from flask import Flask, render_template, url_for, request, redirect, session
from dbSql import ZHIHUDB
import zhihu
import time

app=Flask(__name__, template_folder="./front", static_folder="./front", static_url_path="")
app.secret_key='any random string'

@app.route('/')
def index():
    return render_template("index.html", data=[]) 

@app.route('/Name=<name>')
def search_for_name(name):
    #连接数据库
    DB = ZHIHUDB("localhost", "xiongdb", "root", "280513")
    DB.connect()
    DB.create_tables()
    result = dict()
    #个人信息部分
    session['name'] = name
    result['personal_info'] = DB.get_personal_info(name)
    result['following_info'] = DB.get_following_info(name)
    result['follower_info'] = DB.get_follower_info(name)
    result['answer_info'] = DB.get_answer_info(name)
    result['quiz_info'] = DB.get_quiz_info(name)
    DB.close()
    return render_template("index.html", data=result)

@app.route('/search', methods=['POST', 'GET'])
def search():
    if request.method == 'POST':
        from_name = request.form['name']
        session['name'] = from_name
        zhihu.get_start(from_name)
        time.sleep(10)
        return redirect(url_for('search_for_name', name=from_name))

@app.route('/questionId=<question_id>')
def search_for_question(question_id):
    #连接数据库
    DB = ZHIHUDB("localhost", "xiongdb", "root", "280513")
    DB.connect()
    DB.create_tables()
    result = dict()
    # 贴文信息部分
    result['post_info'] = DB.get_post_info(question_id)
    print(result)
    DB.close()
    return render_template('answer-page.html', data=result)

if __name__=="__main__":
    app.run(port=2020,host="127.0.0.1",debug=False)